﻿#region NeoPatterns
// 
// NeoPatterns framework is released under LGPLv3 license.
// See http://www.opensource.org/licenses/lgpl-3.0.html for more details.
// 
// Please visit http://neopatterns.wikidot.com for more information about NeoPatterns
// 
// Copyright (c) Pascal Craponne 2008
// 
#endregion

namespace NeoPatterns.Core.Aop
{
    /// <summary>
    /// An Aspect is a point cut (to tell if the crosscut applies here) and the advisor to be applied
    /// </summary>
    public class Aspect
    {
        /// <summary>
        /// Tells if the aspect applies here
        /// </summary>
        public IPointcut Pointcut { get; private set; }
        /// <summary>
        /// If the aspect applies, the code to run it
        /// </summary>
        public IAdvisor Advisor { get; private set; }

        /// <summary>
        /// Hashcode needs to be constant if Pointcut and Advisor are
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return (Pointcut != null ? Pointcut.GetHashCode() : 0) ^ Advisor.GetHashCode();
        }

        /// <summary>
        /// Aspect equality is based on Pointcut and Advisor equality
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            var crosscut = obj as Aspect;
            if (crosscut == null)
                return false;
            return Equals(crosscut.Pointcut, Pointcut) && Equals(crosscut.Advisor, Advisor);
        }

        public Aspect(IPointcut pointcut, IAdvisor advisor)
        {
            Advisor = advisor;
            Pointcut = pointcut;
        }
    }
}